#ifndef NETTRANSMISSION_H
#define NETTRANSMISSION_H
#include <mutex>
#include <condition_variable>
#include <string>
#include "TransferCmd.h"
#include "Thread.h"
#include "const.h"
#include "BuffShared.h"
#include "api.h"

using namespace std;

struct StrcParam{
    int ExposureMin;    //曝光等级的最小值
    int ExposureMax;    //曝光等级的最大值
    int ExposureCur = -1;    //当前曝光值
    int Width_tof = 640;    //默认图像宽度
    int Height_tof = 480;
    float fx_tof = 1.0;
    float fy_tof  = 1.0;
    float cx_tof  = 0.0;
    float cy_tof  = 0.0;
    int RgbValid = 0;
};

class NetTransmission 
{
public:
    NetTransmission();
    ~NetTransmission();


public:
    enum TYPEPIPE{
        PIPE_CONTROL,   //用于传输指令
        PIPE_IMG_RAW,       //用于传输原始图像
        PIPE_IMG_SEPARATE,  //用于传输解析后的数据
    };
    string GetServerIP();
    int GetServerPort();
    int Start(string ip , int port, TYPEPIPE type );
    int Stop();
    STRC_IMG_ALL *GetImg();
    char *GetSdkVer();
    char *GetLensInfo();
    void SendCmdGetSdkVer();
    void SendCmdGetLensInfo();
    void SendCmdGetRgbValid();
    int SendCmdGetExposure();
    int GetExposure(int *min, int*max);
    int SetExposure(int level);
    char *GetSNTof();
    StrcParam GetParams();
    int SetFrameSuperposition(int num);
    void SetBlockingActive(bool active);

private : 
    TYPEPIPE mPipeType;
    bool mLoop;
    bool mStoped;
    bool mConnected;
    string mIPServer;   //服务器的IP地址
    int mPortServer;    //服务器的端口
    int mSocket;
    CThread m_LoopThread;
    STRC_IMG_ALL *m_Img;
    CBuffShared *m_ManageImg;
    int m_SizeBuffImg;
    char *m_BuffSdkVer;
    char *m_BuffLensInfo;
    char *m_BuffSNTof;  //tof芯片序列号
    StrcParam mParam;
    bool m_BlockingActive; //是否启用阻塞模式
    //图像阻塞
    std::mutex m_MutexImg;
    std::condition_variable m_CondImg;
    //命令阻塞
    std::mutex m_MutexCmd;
    std::condition_variable m_CondCmd;

    int Connect();
    int ProcessCtlCmd(StrcCmd *cmd);
    int RecLoginResult();   //用于接收登录返回的结果
    int InitImgBuff(int size=NUM_IMG_RECV);
    void FreeImgBuff();
    static void *LoopCtrl(void *param);
    static void *LoopImgSeparate(void *param);
    int DecodeIntrisic(char *intrinsics);

    void SendCmdGetSNTof();

};

#endif // NETTRANSMISSION_H
